package com.example.star.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.star.R;

import java.math.BigDecimal;
import java.util.ArrayList;

public class ProductAdapter extends BaseAdapter {
    private ArrayList<String> imageResources;
    private ArrayList<String> starNames;
    private ArrayList<String> perimeterTitles;
    private ArrayList<String> prices;
    private ArrayList<Integer> quantities;

    private Context context;
    private OnQuantityChangeListener listener;

    public ProductAdapter(ArrayList<String> imageResources, ArrayList<String> starNames, ArrayList<String> perimeterTitles, ArrayList<String> prices, ArrayList<String> quantities) {
        this.imageResources = imageResources;
        this.starNames = starNames;
        this.perimeterTitles = perimeterTitles;
        this.prices = prices;
        this.quantities = new ArrayList<>();
        for (String quantity : quantities) {
            this.quantities.add(Integer.parseInt(quantity));
        }
    }

    public void setOnQuantityChangeListener(OnQuantityChangeListener listener) {
        this.listener = listener;
    }

    public String getQuantity(int position) {
        return String.valueOf(quantities.get(position));
    }

    @Override
    public int getCount() {
        return starNames.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        context = parent.getContext();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_product, parent, false);
        }

        ImageView productImageView = convertView.findViewById(R.id.product_image_view);
        TextView productNameTextView = convertView.findViewById(R.id.product_name_text_view);
        TextView productPriceTextView = convertView.findViewById(R.id.product_price_text_view);
        Button decreaseButton = convertView.findViewById(R.id.decrease_button);
        TextView quantityTextView = convertView.findViewById(R.id.quantity_text_view);
        Button increaseButton = convertView.findViewById(R.id.increase_button);
        TextView totalPriceTextView = convertView.findViewById(R.id.total_price_text_view);

        String imageResource = imageResources.get(position);
        String perimeterTitle = perimeterTitles.get(position);
        String price = prices.get(position);
        int quantity = quantities.get(position);
        BigDecimal totalPrice = new BigDecimal(price).multiply(new BigDecimal(quantity));

        productNameTextView.setText(perimeterTitle);
        productPriceTextView.setText("单价: " + price + "元");
        quantityTextView.setText(String.valueOf(quantity));
        totalPriceTextView.setText(totalPrice.toString());

        // 加载商品图片
        if (imageResource != null) {
            try {
                int imageResId = context.getResources().getIdentifier(imageResource, "drawable", context.getPackageName());
                if (imageResId != 0) {
                    productImageView.setImageResource(imageResId);
                } else {
                    Glide.with(context)
                            .load(imageResource)
                            .placeholder(R.drawable.register)
                            .error(R.drawable.register)
                            .into(productImageView);
                }
            } catch (Exception e) {
                Glide.with(context)
                        .load(imageResource)
                        .placeholder(R.drawable.register)
                        .error(R.drawable.register)
                        .into(productImageView);
            }
        }

        decreaseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (quantity > 1) {
                    quantities.set(position, quantity - 1);
                    notifyDataSetChanged();
                    if (listener != null) {
                        listener.onQuantityChanged();
                    }
                }
            }
        });

        increaseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quantities.set(position, quantity + 1);
                notifyDataSetChanged();
                if (listener != null) {
                    listener.onQuantityChanged();
                }
            }
        });

        return convertView;
    }

    public interface OnQuantityChangeListener {
        void onQuantityChanged();
    }
}